package com.icss.ah.purchaseGoods.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.ah.base.bean.EasyuiDataGridJson;

@Service
@Transactional
public class PurchaseGoodsService {
	
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	public List<Map<String, Object>> getProductTypeList(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("PurchaseGoodsBean.getProductTypeList",map);
			infolog.info("PurchaseGoodsService|getProductTypeList|para:"+map);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PurchaseGoodsService|getProductTypeList",e);
		}
		return list;
	}
	
	public EasyuiDataGridJson queryPurchaseGoodsList(Map<String, String> map){
		EasyuiDataGridJson dataGridJson = new EasyuiDataGridJson();
		try {
			List<Map<String, Object>> rows = sqlSessionTemplate.selectList("PurchaseGoodsBean.queryPurchaseGoodsList",map);
			Long total = sqlSessionTemplate.selectOne("PurchaseGoodsBean.queryPurchaseGoodsListCount",map);
			dataGridJson.setRows(rows);
			dataGridJson.setTotal(total);
			infolog.info("PurchaseGoodsService|queryPurchaseGoodsList|para:"+map);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PurchaseGoodsService|queryPurchaseGoodsList",e);
		}
		return dataGridJson;
	}
	
	public void productUp(Map<String, String> map){
		try {
			sqlSessionTemplate.update("PurchaseGoodsBean.productUp",map);
			infolog.info("PurchaseGoodsService|productUp|para:"+map);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PurchaseGoodsService|productUp",e);
		}
	}
	
	public void productDel(Map<String, String> map){
		try {
			sqlSessionTemplate.update("PurchaseGoodsBean.productDel",map);
			infolog.info("PurchaseGoodsService|productDel|para:"+map);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PurchaseGoodsService|productDel",e);
		}
	}
	
	public EasyuiDataGridJson queryProductInfoList(Map<String, String> map){
		EasyuiDataGridJson dataGridJson = new EasyuiDataGridJson();
		try {
			List<Map<String, Object>> rows = sqlSessionTemplate.selectList("PurchaseGoodsBean.queryProductInfoList",map);
			Long total = sqlSessionTemplate.selectOne("PurchaseGoodsBean.queryProductInfoListCount",map);
			dataGridJson.setRows(rows);
			dataGridJson.setTotal(total);
			infolog.info("PurchaseGoodsService|queryPurchaseGoodsList|para:"+map);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PurchaseGoodsService|queryPurchaseGoodsList",e);
		}
		return dataGridJson;
	}
	
	public String saveSearchProductInfo(Map<String, String> map){
		String result = "0, ";
		try {
			String uuids = map.get("uuids").toString();
			String[] productId = uuids.split(",");
			for(int i =0 ; i<productId.length;i++){
				String productInfo = productId[i];
				String[] product = productInfo.split("#@");
				map.put("oldShopId", product[0]);
				map.put("productId", product[1]);
				map.put("newProductId", UUID.randomUUID().toString());
				sqlSessionTemplate.update("PurchaseGoodsBean.saveSearchProductInfo", map);
				infolog.info("PurchaseGoodsService|saveSearchProductInfo|para:"+map);
			}
			result = "1,保存成功";
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PurchaseGoodsService|saveSearchProductInfo",e);
			result = "2,保存失败："+e.getMessage();
		}
		return result;
	}

}
